//
//  String+Extension.swift
//  swift3.0Exercise
//
//  Created by Aaron on 2016/12/22.
//  Copyright © 2016年 com.solidInfo. All rights reserved.
//

import UIKit
import CoreFoundation

extension String {
   
    ///  拼音字符串
    var pinyinCharactor : String! {
        let transformContents = CFStringCreateMutableCopy(nil, 0, self as CFString)
                //先转换为带声调的拼音
        CFStringTransform(transformContents, nil, kCFStringTransformMandarinLatin, false)
                //再转换为不带声调的拼音
        CFStringTransform(transformContents, nil, kCFStringTransformStripDiacritics, false)
        
        var ztransformContents = String(describing: transformContents)
        
        ztransformContents = ztransformContents.replacingOccurrences(of: "Optional(", with: "")
        ztransformContents = ztransformContents.replacingOccurrences(of: ")", with: "")
        
        return ztransformContents.uppercased()
    }
    
    ///  拼音首字母
    var pinyinFirstCharactor : String! {
        let pinyin = (pinyinCharactor as String).uppercased() as NSString
        return pinyin.substring(to: 1)
    }

    /// UTF-8编码
    var UTF8String: String! {
        return addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed)
    }

//    func textSize(font: UIFont!, boundingSize: CGSize) -> CGSize {
//        return self.boundingRect(with: boundingSize, options: .usesLineFragmentOrigin, attributes: [FontAttributeName : font], context: nil).size
//    }

}

